﻿using System;
using Microsoft.Win32.SafeHandles;
using System.Runtime.InteropServices;

namespace _7_3_Class
{
    public interface IMyBaseInterface
    {
        //属性方法
        float X
        {
            set;
            get;
        }
        //成员方法
        void DoSomething();
        //1.不能使用public private protected internal 修饰接口成员
        //2.不能使用static virtual abstract sealed 修饰接口成员
        //3.不能定义字段成员
        //4.不能有方法体
        //
    }

    public interface IMyInterface : IMyBaseInterface
    {
        new void DoSomething();
    }

    public class TestI1 : IMyBaseInterface
    {
        public float X
        {
            get
            {
                throw new NotImplementedException();
            }

            set
            {
                throw new NotImplementedException();
            }
        }

        public void DoSomething()
        {
            Console.WriteLine("TestI1 DoSomething") ;
        }
    }

    public class TestI2 : IMyInterface
    {
        float IMyBaseInterface.X
        {
            get
            {
                throw new NotImplementedException();
            }

            set
            {
                throw new NotImplementedException();
            }
        }

        void IMyBaseInterface.DoSomething()
        {
            throw new NotImplementedException();
        }
        void IMyInterface.DoSomething()
        {
            throw new NotImplementedException();
        }
    }



    public class MyBaseClass
    {

        public class Vec2
        {
            public int x;
            public int y;
            public override string ToString()
            {
                return "x: " + x + " y:" + y;
            }
        }
        public void DoSomething (Vec2 xy)
        {
            Console.WriteLine("MyBaseClass DoSomething xy: " + xy);
        }

        

        public virtual void Do()
        {
            Console.WriteLine("MyBaseClass Do");
        }
    }

    public class MyClass : MyBaseClass
    {
        //隐藏
        public new void DoSomething()
        {
            Console.WriteLine("MyClass DoSomething");
        }

        //重写
        public override void Do()
        {
            Console.WriteLine("MyClass Do");
        }
    }

    //密封
    public sealed class SealedClass
    {
        
    }

    class TestClass
    {
        public const int testI1 = 1;
        public int testI2;
        public static int testI3;
        public readonly int testI4 = 14;
        public void TestDisposable()
        {
            
            TestI1 ti1 = new TestI1();
            TestI2 ti2 = new TestI2();

            ti1.DoSomething();

            IMyInterface iiii;
            iiii = ti2;
            iiii.DoSomething();

            IMyBaseInterface imbi;
            imbi = ti1;
            imbi.DoSomething();
            imbi = ti2;
            imbi.DoSomething();

            IMyInterface imi;
            //imi = ti1;
            //imi.DoSomething();
            imi = ti2;
            imi.DoSomething();

            Console.ReadKey();

            MyClass mc = new MyClass();
            MyBaseClass mbc;

            mc.DoSomething();
            mc.Do();

            mbc = mc;
            mbc.DoSomething(new MyClass.Vec2());
            mbc.Do();


            Console.ReadKey();
            BaseClass bc1 = new BaseClass();

            //using (bc1)
            using (BaseClass bc2 = new BaseClass())
            {

            }
        }
    }


    //可删除的对象
    class BaseClass : IDisposable
    {
        // Flag: Has Dispose already been called?
        bool disposed = false;

        // Public implementation of Dispose pattern callable by consumers.
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        // Protected implementation of Dispose pattern.
        protected virtual void Dispose(bool disposing)
        {
            if (disposed)
                return;

            if (disposing)
            {
                // Free any other managed objects here.
                //
            }

            // Free any unmanaged objects here.
            //
            disposed = true;
        }

        ~BaseClass()
        {
            Dispose(false);
        }
    }
}
